package com.lkd.job;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyChannel;
import com.lkd.contract.SupplyContract;
import com.lkd.dao.ChannelDao;
import com.lkd.dao.VendingMachineDao;
import com.lkd.emq.MqttProducer;
import com.lkd.entity.ChannelEntity;
import com.lkd.entity.VendingMachineEntity;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-08-22 9:30
 */
@Component
@Slf4j
public class SupplyTaskJob {

    @Autowired
    private ChannelDao channelDao;
    @Autowired
    private MqttProducer mqttProducer;
    @Autowired
    private VendingMachineDao vendingMachineDao;


    @XxlJob("supplyTask")
    public ReturnT<String> supplyTask(String param) {
        try{

            //总分片数 3
            int shardTotal = XxlJobHelper.getShardTotal();
            //当前分片序号 0
            int shardIndex = XxlJobHelper.getShardIndex();

            System.out.println("总分片数：" + shardTotal +" 当前分片序号:" + shardIndex);

            //获得需要补货货道信息  100  1-1 1-2  101 1-2
            List<ChannelEntity> channelEntities = channelDao.getUnfullChannel(shardIndex,shardTotal);

            if(CollectionUtils.isEmpty(channelEntities)){
                return ReturnT.SUCCESS;
            }

            //获取所有售货机编号  100 101
            List<String> innerCodes = channelEntities.stream().map(ChannelEntity::getInnerCode).distinct().collect(Collectors.toList());

            //遍历所有售货机编号
            for (String innerCode : innerCodes) {

                SupplyContract supplyContract = new SupplyContract();
                supplyContract.setInnerCode(innerCode);

                VendingMachineEntity vendingMachineEntity = vendingMachineDao.findByInnerCode(innerCode);
                supplyContract.setRegionId(vendingMachineEntity.getRegionId());

                List<SupplyChannel> channels =
                        channelEntities.stream().
                                filter(data -> data.getInnerCode().equals(innerCode))
                                .map(x -> {
                                    SupplyChannel supplyChannel = new SupplyChannel();
                                    supplyChannel.setChannelId(x.getChannelCode());
                                    supplyChannel.setCapacity(x.getMaxCapacity() - x.getCurrentCapacity());
                                    supplyChannel.setSkuId(x.getSkuId());
                                    if(x.getSku() != null){
                                        supplyChannel.setSkuName(x.getSku().getSkuName());
                                        supplyChannel.setSkuImage(x.getSku().getSkuImage());
                                    }
                                    return supplyChannel;
                                })
                                .collect(Collectors.toList());

                supplyContract.setSupplyData(channels);
                mqttProducer.send(TopicConfig.TASK_SUPPLY_TOPIC,2,supplyContract);

                System.out.println("处理了" + innerCode);
            }


            return ReturnT.SUCCESS;
        }catch (Exception e){
            log.error("定时创建补货工单失败",e);
            return ReturnT.FAIL;
        }
    }
}
